﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.User;
using Enterprise.Data.User;
using Enterprise.Infrastructure;
namespace Enterprise.Service.User
{
    /// <summary>
    ///  用户角色业务服务
    /// </summary>
    public class UserRoleService
    {
        /// <summary>
        /// 用户角色缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-UserRole";

        /// <summary>
        /// 用户角色列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public static List<UserRoleEntity> UserRoleList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<UserRoleEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                UserRoleData data = new UserRoleData();
                List<UserRoleEntity> list = data.UserRoleList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 获取一个用户的所有角色信息,不使用缓存数据(使用缓存有可能导致性能下降)
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public static List<UserRoleEntity> UserRoleList(int UserId)
        {
            List<UserRoleEntity> list = new List<UserRoleEntity>();           
            UserRoleData data = new UserRoleData();
            list = data.UserRoleList(UserId);
            return list.Where(s => s.USERID == UserId).ToList();
        }

        /// <summary>
        ///  判断用户是否属于角色
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static bool UserRoleCheck(int UserId, int RoleId)
        {
            bool rbool = false;
            var q = UserRoleList().Where(p => p.USERID == UserId && p.ROLEID == RoleId).FirstOrDefault();
            if (q != null)
            {
                rbool = true;
            }
            return rbool;
        }

        /// <summary>
        /// 判断用户是否属于角色集合
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="RoleIds"></param>
        /// <returns></returns>
        public static bool UserRoleCheck(int UserId, List<int> RoleIds)
        {
            List<UserRoleEntity> list = UserRoleList(UserId);
            bool rbool = false;
            foreach (int RoleId in RoleIds)
            {
                rbool=list.Exists(p => p.USERID == UserId && p.ROLEID == RoleId)?true:false;
            }
            return rbool;
        }

        /// <summary>
        /// 人员角色表操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void UserRoleExecute(UserRoleEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_USERROLE(USERID,ROLEID) values(" + entity.USERID + "," + entity.ROLEID + ")";
                    break;
                case "Update":
                    //cmd = "Update SYS_USERROLE set UID=" + entity.USERID + " and RID=" + entity.ROLEID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_USERROLE where USERID=" + entity.USERID + " and ROLEID=" + entity.ROLEID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                UserRoleExecuteBySql(cmd);
            }
        }

        /// <summary>
        /// 人员操作  
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void UserRoleExecuteBySql(string cmd)
        {
            UserData data = new UserData();
            data.UserExecute(cmd);
            CacheHelper.RemoveCache(cacheKey);
        }
    }
}
